package sparrowGui.skin
{
	import flash.display.DisplayObject;
	import flash.display.Shape;
	import flash.events.Event;
	
	import sparrowGui.Component;
	import sparrowGui.ComponentDraw;
	import sparrowGui.SparrowGUI;
	import sparrowGui.VScrollBar;
	import sparrowGui.item.Item;
	import sparrowGui.utils.PubFun;
	import sparrowGui.vars.BaseComponentVars;
	
	public class HScrollBarSkin extends Component
	{
		public var leftBtn:Item;
		public var rightBtn:Item;
		public var slider:Item;
		public var skinbg:DisplayObject;
		
		public function HScrollBarSkin()
		{
/*			var vars:ComponentVars = new ComponentVars();
			vars.bgColor = 0xCCCCCC;
			vars.setBorder(1);
			vars.setBorderColor(0x666666);
			vars.width = vars.height = VScrollBar.THICKNESS;*/
			
			super();
			
			leftBtn.onInvalidate(null);
			rightBtn.onInvalidate(null);
			slider.onInvalidate(null);
			
//			leftBtn.visible = false;
//			rightBtn.visible = false;
			
//			forceDraw = true;
		}
		
		override protected function init():void
		{
/*			var vars:ComponentVars = new ComponentVars();
			vars.alpha = 0.2;
			vars.bgColor = 0xFFFFFF;*/
			skinbg = new ComponentDraw(SparrowGUI.getIns().scrollBarVar);
			skinbg.width = VScrollBar.THICKNESS;
			skinbg.name = "skinbg";
			
			var btnSty:BaseComponentVars = new BaseComponentVars();
			btnSty.width = btnSty.height = VScrollBar.THICKNESS;
			btnSty.isBgFill = true;
			
			leftBtn = new Item(null,null,btnSty);
			leftBtn.name = "leftBtn";
			rightBtn = new Item(null,null,btnSty);
			rightBtn.name = "rightBtn";
			
			var bs:Object = PubFun.copyObj(btnSty);
			slider = new Item(null,null,bs);
			bs.isIntXY = false;
			slider.isBgFill = true;
			slider.name = "slider";
			slider.width = VScrollBar.THICKNESS;
			slider.height = VScrollBar.THICKNESS;
			
			var arrSp:Shape = PubFun.drawArrow(2);
			arrSp.x = arrSp.y = 2;
			leftBtn.addChild(arrSp);
			
			arrSp = PubFun.drawArrow(3);
			arrSp.x = arrSp.y = 2;
			rightBtn.addChild(arrSp);
			
			addChild(skinbg);
			addChild(slider);
			addChild(leftBtn);
			addChild(rightBtn);
		}
		
		private function refush():void
		{
			leftBtn.setSize(VScrollBar.THICKNESS,VScrollBar.THICKNESS);
			leftBtn.y = 0;
			
			rightBtn.setSize(VScrollBar.THICKNESS,VScrollBar.THICKNESS);
			rightBtn.y = style.height - VScrollBar.THICKNESS;
			
			var minH:int = VScrollBar.THICKNESS*2 + VScrollBar.MIN_SLIDER_HEIGHT;
			if(style.height<minH){
				slider.visible = false;
				return;
			}
			slider.setSize(VScrollBar.THICKNESS,(style.height-VScrollBar.THICKNESS*2));
			slider.y = VScrollBar.THICKNESS;
		}
		
		override protected function draw():void
		{
			if(leftBtn){
				leftBtn.x = style.paddingLeft;
				leftBtn.y = style.paddingTop;
				leftBtn.setSize(VScrollBar.THICKNESS,VScrollBar.THICKNESS);
			}
			if(rightBtn){
				rightBtn.y = 0;
				rightBtn.x = (style.width - rightBtn.width);
				rightBtn.setSize(VScrollBar.THICKNESS,VScrollBar.THICKNESS);
			}
			if(slider){
				slider.y = style.paddingTop;
				slider.x = leftBtn.x + leftBtn.width;
//				slider.setSize(ScrollBar.THICKNESS,ScrollBar.MIN_SLIDER_HEIGHT);
				var minH:int = VScrollBar.THICKNESS*2 + VScrollBar.MIN_SLIDER_HEIGHT;
				if(style.width<minH){
					slider.visible = false;
					return;
				}
			}
//			if(skinbg){
//				skinbg.width = style.width;
//			}
			super.draw();
		}
		
		override public function set width(w:Number):void
		{
			style.width = w;
			skinbg.width = w;
			draw();
			dispatchEvent(new Event(Event.RESIZE));
		}
	}
}